﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION f_Is_PU_Has_Inv_Pay
(
	@ID int
)
RETURNS bit
AS
BEGIN
	-- Declare the return variable here
	DECLARE @ResultVar bit
	-- 0 не имеет связи
	-- 1 имеет счета
	-- 2 имеет платежи 
	-- 3 имеет и счета и платежи
	
	SET @ResultVar = 0

	IF  (EXISTS (SELECT TOP 1 ID FROM INVOICE_OUT_ITEM as i WHERE i.PRODUCT_UNIT_ID = @ID) 
		OR
		EXISTS (SELECT TOP 1 ID FROM PAYMENT_IN_ITEM as p WHERE p.PRODUCT_UNIT_ID = @ID))
	BEGIN
		SET @ResultVar = 1
	END

	-- Return the result of the function
	RETURN @ResultVar

END